package io.renren.modules.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.ImGroupEntity;
import io.renren.modules.app.entity.ImGroupUserEntity;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.ImGroupService;
import io.renren.modules.app.service.ImGroupUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *  群
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-05-19 09:14:52
 */
@RestController
@RequestMapping("app/imgroup")
public class ImGroupController {
    @Autowired
    private ImGroupService imGroupService;
    @Autowired
    private ImGroupUserService imGroupUserService;

    /**
     * 验证
     */
    @Login
    @RequestMapping("/check")
    //@RequiresPermissions("generator:imgroup:list")
    public R check(@LoginUser UserEntity user ,@RequestBody ImGroupEntity imGroup){
        imGroup.setId(imGroup.getUserId());
        R r = imGroupService.check(user,imGroup);
        return r;
    }
    /**
     * 列表
     */
    @Login
    @RequestMapping("/list")
    //@RequiresPermissions("generator:imgroup:list")
    public R list(@LoginUser UserEntity user , @RequestParam Map<String, Object> params){
        //PageUtils page = imGroupService.queryPage(params);
        List<ImGroupEntity> list = imGroupService.getList(user);
        return R.ok().put("page", list);
    }

    /**
     * 创建
     */
    @Login
    @RequestMapping("/create")
    //@RequiresPermissions("generator:imgroup:list")
    public R create(@LoginUser UserEntity user , @RequestBody Map<String,String []> map) throws Exception {
        //PageUtils page = imGroupService.queryPage(params);
        // bean.setUserId(user.getUserId().intValue());

        return R.ok(imGroupService.create(user,map));
    }

    /**
     * 信息
     */
    @Login
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("generator:imgroup:info")
    public R info(@LoginUser UserEntity user ,@PathVariable("id") Integer id){
        R r = imGroupService.info(user,id);
        return r;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:imgroup:save")
    public R save(@RequestBody ImGroupEntity imGroup){
		imGroupService.save(imGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @Login
    @RequestMapping("/update")
    //@RequiresPermissions("generator:imgroup:update")
    public R update(@LoginUser UserEntity user ,@RequestBody ImGroupEntity imGroup){
        R r = imGroupService.update(user,imGroup);
		//imGroupService.updateById(imGroup);

        return r;
    }

    /**
     * 修改名称
     * @param user
     * @param imGroup
     * @return
     */
    @Login
    @RequestMapping("/nickname")
    //@RequiresPermissions("generator:imgroup:update")
    public R nickname(@LoginUser UserEntity user ,@RequestBody ImGroupEntity imGroup){
        R r = imGroupService.nickname(user,imGroup);
        //imGroupService.updateById(imGroup);

        return r;
    }
    /**
     * 修改公告
     * @param user
     * @param imGroup
     * @return
     */
    @Login
    @RequestMapping("/remark")
    //@RequiresPermissions("generator:imgroup:update")
    public R remark(@LoginUser UserEntity user ,@RequestBody ImGroupEntity imGroup){
        R r = imGroupService.remark(user,imGroup);
        //imGroupService.updateById(imGroup);

        return r;
    }
    /**
     * 删除或退出该群聊
     * @param user
     * @param imGroup
     * @return
     */
    @Login
    @RequestMapping("/quit")
    //@RequiresPermissions("generator:imgroup:update")
    public R quit(@LoginUser UserEntity user ,@RequestBody ImGroupEntity imGroup){
        R r = imGroupService.quit(user,imGroup);
        //imGroupService.updateById(imGroup);

        return r;
    }
    /**
     * 邀请加入群聊
     * @param user
     * @param imGroup
     * @return
     */
    @Login
    @RequestMapping("/invite")
    //@RequiresPermissions("generator:imgroup:update")
    public R invite(@LoginUser UserEntity user ,@RequestBody ImGroupEntity imGroup){
        R r = imGroupService.invite(user,imGroup);
        //imGroupService.updateById(imGroup);

        return r;
    }
    /**
     * 踢出
     * @param user
     * @param imGroup
     * @return
     */
    @Login
    @RequestMapping("/kickoff")
    //@RequiresPermissions("generator:imgroup:update")
    public R kickoff(@LoginUser UserEntity user ,@RequestBody ImGroupEntity imGroup){
        R r = imGroupService.kickoff(user,imGroup);
        //imGroupService.updateById(imGroup);

        return r;
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:imgroup:delete")
    public R delete(@RequestBody Integer[] ids){
		imGroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
